/*!
 * vinculum
 * Copyright(c) 2017 azzly
 * MIT Licensed
 */
 
'use strict';

/**
 * Module dependencies.
 * @private
 */
const helper = require("../helper");
const CONST = require("../CONST");
const config = require("../config");
const debug = require("../debug")(__filename);
const error = require("../HTTPErrors");
const datasource = require("../datasource");
const application = require("../oo/application");

const inser_log = (id,log_params)=>{
  application.find(id).then(info=>{
    if(info.islog === CONST.LOG_TYPES.LOG){
			datasource.systemQuery(CONST.SQLS.log_insert,log_params);
    }
  });
};
 
/**
 * module.
 */

module.exports = ()=>{
  return (req,res,next)=>{
		let id = req.params.id;
		let params = req.vinculum.params;
		let log_params = [
			helper.uuid()
			,id
			,'application'
			,params.__request_header__ip
			,params.__session_user__id
			,helper.json2string(params)
		];
		new application(id).render(params,req.query).then(result=>{
			log_params.push('success');
			inser_log(id,log_params);
			res.vinculum.html(result);
		}).catch(err=>{
			log_params.push(helper.json2string(err));
			inser_log(id,log_params);
			res.vinculum.error(err);
		});
  }
};